//
//  WaitTakeDetailViewController.m
//  rrqs
//
//  Created by 杜文杰 on 2018/8/24.
//

#import "WaitTakeDetailViewController.h"
#import "OrderDetailCell.h"
#define BGCOLOR "F1F1F1"

@interface WaitTakeDetailViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *mainTableView; // 主框架
@property (nonatomic, strong) UIView *headerView;
@property (nonatomic, strong) UIView *footerView;
@property (nonatomic, strong) UILabel *orderIdLabel; // 订单编号
@property (nonatomic, strong) UILabel *payTypeLabel;// 支付方式
@property (nonatomic, strong) UILabel *titleLabel;// 店铺名
@property (nonatomic, strong) UILabel *dateLabel; // 下单日期
@property (nonatomic, strong) UILabel *totalMoneyLabel;// 总计金额
@property (nonatomic, strong) UIButton *bottomBtn; // 地址
@property (nonatomic, strong) NSArray *dataArray; //
@property (nonatomic, strong) NSString *site_phoneStr; // 联系商家
@property (nonatomic, strong) NSString *gkPhoneStr; // 联系顾客
@property (nonatomic, strong) UIButton *sjBtn; // 联系商家
@property (nonatomic, strong) UIButton *gkBtn; // 联系顾客
@property (nonatomic, strong) UIButton *makeSureBtn; // 确认取货
@end

@implementation WaitTakeDetailViewController
// 界面数据源
- (void)queryData {
    AFHTTPRequestOperationManager* mgr = [AFHTTPRequestOperationManager manager];
//    NSDictionary* arg = @{@"ince":@"get_order_info",@"order_id":self.waiMaiDodelHList.order_id,@"emp_id":[UserModel sharedInstanced].userId};
        NSDictionary* arg = @{@"ince":@"get_order_infos",@"main_id":self.waiMaiDodelListitems.order_id,@"emp_id":[UserModel sharedInstanced].userId};
    [mgr POST:kHost parameters:arg success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
        [WMHelper outPutJsonString:responseObject];
        NSInteger flag = [[responseObject objectForKey:@"status"]integerValue];
        if(flag == 1){
            NSDictionary* dict = [responseObject objectForKey:@"order"];
            NSArray *array = dict[@"items"];
            
            self.orderIdLabel.text = [NSString stringWithFormat:@"订单号: %@",dict[@"main_id"]];
            if (![WMHelper isNULLOrnil:array]) {
                NSDictionary *dataDic = array[0];
                self.titleLabel.text = dataDic[@"site_name"];
                if ([dataDic[@"paytype"] isEqualToString:@"2"]) {
                    self.payTypeLabel.text = @"微信支付";
                }else
                {
                    self.payTypeLabel.text = @"其他支付";
                }
                self.dateLabel.text = dataDic[@"add_time"];
                self.totalMoneyLabel.text = dataDic[@"food_amount"];
                [self.bottomBtn setTitle:dataDic[@"site_addr"] forState:UIControlStateNormal];
                self.dataArray = dataDic[@"foods"];
                self.site_phoneStr = dataDic[@"site_phone"];
                self.gkPhoneStr = dataDic[@"phone"];
//                if ([dataDic[@"status"] isEqualToString:@"7"]) {
//                    [self.makeSureBtn setTitle:@"配送中" forState:UIControlStateNormal];
//                    [self.makeSureBtn setBackgroundColor:getColor(@BGCOLOR)];
//                    self.makeSureBtn .enabled = YES;
//                }
            }
            
            
            [self.mainTableView reloadData];
            [self.mainTableView.mj_header endRefreshing];
        }else{
            [self alertHUD:[responseObject objectForKey:@"fail"]];
            [self.navigationController popViewControllerAnimated:YES];
        }
    } failure:^(AFHTTPRequestOperation * _Nonnull operation, NSError * _Nonnull error) {
        [self alertHUD:@"网络异常"];
        [self.mainTableView.mj_header endRefreshing];
    }];
}
- (void)makeSureQueryData {
    AFHTTPRequestOperationManager* mgr = [AFHTTPRequestOperationManager manager];
    NSDictionary* arg = @{@"ince":@"qs_get_food",@"order_id":self.waiMaiDodelListitems.order_id,@"emp_id":[UserModel sharedInstanced].userId};
    [mgr POST:kHost parameters:arg success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
        [self queryData];
        NSLog(@"成功");
    } failure:^(AFHTTPRequestOperation * _Nonnull operation, NSError * _Nonnull error) {
        [self alertHUD:@"网络异常"];
    }];
}

- (void) btnAction:(UIButton *)btn
{
    if ([btn isEqual:self.sjBtn]) {
        [self callPhone:self.site_phoneStr];
    }
    if ([btn isEqual:self.gkBtn]) {
        [self callPhone:self.gkPhoneStr];
    }
    if ([btn isEqual:self.makeSureBtn]) {
        if (self.isPeiSongz) { // 配送中  确认送达 接口
            
        }else{
            [self makeSureQueryData]; // 待取货   确认取货 接口
        }
    }
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"订单详情";
    [self queryData];
    [self.view addSubview:self.mainTableView];
    if (self.isPeiSongz) { // 配送中  确认送达
        [self.makeSureBtn setTitle:@"确认送达" forState:UIControlStateNormal];
    }
}
#pragma mark -UITableViewDelegate,UITableViewDataSource
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 40 / HEIGHT_6S_SCALE;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    OrderDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([OrderDetailCell class])];
    cell.dataDic = self.dataArray[indexPath.row];
    // 横线
    UIView *lineView2 = [[UIView alloc]initWithFrame:CGRectZero];
    lineView2.backgroundColor = getColor(@BGCOLOR);
    [cell.contentView addSubview:lineView2];
    [lineView2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(cell.contentView.mas_bottom).offset(-1 / HEIGHT_6S_SCALE);
        make.left.right.equalTo(cell.contentView);
        make.height.mas_equalTo(1 / HEIGHT_6S_SCALE);
    }];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.01f;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01f;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 0.01)];
    return view;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 0.01)];
    return view;
}
#pragma mark - init
- (UITableView *)mainTableView
{
    if (!_mainTableView) {
        _mainTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - SafeAreaBottomHeight - SafeAreaTopHeight) style:UITableViewStyleGrouped];
        _mainTableView.backgroundColor = getColor(@BGCOLOR);
        _mainTableView.tableHeaderView = self.headerView;
        _mainTableView.tableFooterView = self.footerView;
        _mainTableView.delegate = self;
        _mainTableView.dataSource = self;
        _mainTableView.showsVerticalScrollIndicator = NO;
        _mainTableView.showsHorizontalScrollIndicator = NO;
        _mainTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        // 动态行高
        _mainTableView.rowHeight = UITableViewAutomaticDimension;
        // 预估行高
        _mainTableView.estimatedRowHeight = 80;
        [_mainTableView registerClass:[OrderDetailCell class] forCellReuseIdentifier:NSStringFromClass([OrderDetailCell class])];
        //刷新、加载
        _mainTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            [self queryData];
        }];
    }
    return _mainTableView;
}
- (UIView *)headerView
{
    if (!_headerView) {
        _headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 90 / HEIGHT_6S_SCALE)];
        _headerView.backgroundColor = [UIColor whiteColor];
        __weak typeof(self) weakself = self;
        // 订单编号
        UILabel *orderIdLabel = [[UILabel alloc]init];
        orderIdLabel.backgroundColor = getColor(whiteColor);
        orderIdLabel.font = DEF_FontSize_14;
        orderIdLabel.textAlignment = NSTextAlignmentLeft;
//        orderIdLabel.text = @"订单号:201838374474848112";
        self.orderIdLabel = orderIdLabel;
        [_headerView addSubview:orderIdLabel];
        [orderIdLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_headerView.mas_top).offset(10 / HEIGHT_6S_SCALE);
            make.left.equalTo(_headerView.mas_left).offset(10 / WIDTH_6S_SCALE);
            make.height.mas_equalTo(20 / HEIGHT_6S_SCALE);
            make.width.mas_equalTo(SCREEN_WIDTH - 80 / WIDTH_6S_SCALE - 20 / WIDTH_6S_SCALE);
        }];
        // 支付方式
        UILabel *payTypeLabel = [[UILabel alloc]init];
        payTypeLabel.backgroundColor = getColor(whiteColor);
        payTypeLabel.font = DEF_FontSize_14;
        payTypeLabel.textAlignment = NSTextAlignmentRight;
//        payTypeLabel.text = @"微信支付";
        self.payTypeLabel = payTypeLabel;
        [_headerView addSubview:payTypeLabel];
        [payTypeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(weakself.orderIdLabel);
            make.right.equalTo(_headerView.mas_right).offset(-10 / WIDTH_6S_SCALE);
            make.height.mas_equalTo(20 / HEIGHT_6S_SCALE);
            make.width.mas_equalTo(80 / WIDTH_6S_SCALE);
        }];
        // 横线
        UIView *lineView1 = [[UIView alloc]initWithFrame:CGRectZero];
        lineView1.backgroundColor = getColor(@BGCOLOR);
        [_headerView addSubview:lineView1];
        [lineView1 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(weakself.orderIdLabel.mas_bottom).offset(10 / HEIGHT_6S_SCALE);
            make.left.right.equalTo(weakself.headerView);
            make.height.mas_equalTo(10 / HEIGHT_6S_SCALE);
        }];
        // 店名
        UILabel *titleLabel = [[UILabel alloc]init];
        titleLabel.backgroundColor = getColor(whiteColor);
        titleLabel.font = DEF_FontSize_14;
        titleLabel.textAlignment = NSTextAlignmentLeft;
        titleLabel.textColor = [UIColor redColor];
//        titleLabel.text = @"鸿鑫手擀面";
        self.titleLabel = titleLabel;
        [_headerView addSubview:titleLabel];
        [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(lineView1.mas_bottom).offset(10 / HEIGHT_6S_SCALE);
            make.left.equalTo(_headerView.mas_left).offset(10 / WIDTH_6S_SCALE);
            make.height.mas_equalTo(20 / HEIGHT_6S_SCALE);
            make.width.mas_equalTo(SCREEN_WIDTH - 100 / WIDTH_6S_SCALE - 20 / WIDTH_6S_SCALE);
        }];
        // 日期时间
        UILabel *dateLabel = [[UILabel alloc]init];
        dateLabel.backgroundColor = getColor(whiteColor);
        dateLabel.font = DEF_FontSize_14;
        dateLabel.textAlignment = NSTextAlignmentRight;
//        dateLabel.text = @"08-24 09:38:44";
        self.dateLabel = dateLabel;
        [_headerView addSubview:dateLabel];
        [dateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(weakself.titleLabel);
            make.right.equalTo(_headerView.mas_right).offset(-10 / WIDTH_6S_SCALE);
            make.height.mas_equalTo(20 / HEIGHT_6S_SCALE);
            make.width.mas_equalTo(120 / WIDTH_6S_SCALE);
        }];
        // 横线
        UIView *lineView2 = [[UIView alloc]initWithFrame:CGRectZero];
        lineView2.backgroundColor = getColor(@BGCOLOR);
        [_headerView addSubview:lineView2];
        [lineView2 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(titleLabel.mas_bottom).offset(10 / HEIGHT_6S_SCALE);
            make.left.right.equalTo(_headerView);
            make.height.mas_equalTo(1 / HEIGHT_6S_SCALE);
        }];
        
    }
    return _headerView;
}
- (UIView *)footerView
{
    if (!_footerView) {
        _footerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 150)];
        _footerView.backgroundColor = [UIColor whiteColor];
        __weak typeof(self) weakself = self;
        
        UILabel *totalPayLabel = [[UILabel alloc]init];
        totalPayLabel.textColor = getColor(textColor);
        totalPayLabel.font = DEF_FontSize_14;
        totalPayLabel.textAlignment = NSTextAlignmentLeft;
        totalPayLabel.text = @"总计";
        [_footerView addSubview:totalPayLabel];
        [totalPayLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_footerView.mas_top).offset(10 / HEIGHT_6S_SCALE);
            make.left.equalTo(_footerView.mas_left).offset(10 / WIDTH_6S_SCALE);
            make.height.mas_equalTo(20 / HEIGHT_6S_SCALE);
            make.width.mas_equalTo(SCREEN_WIDTH - 80 / WIDTH_6S_SCALE - 20 / WIDTH_6S_SCALE);
        }];

        // 总计金额
        UILabel *totalMoneyLabel = [[UILabel alloc]init];
        totalMoneyLabel.textColor = getColor(textColor);
        totalMoneyLabel.font = DEF_FontSize_14;
        totalMoneyLabel.textAlignment = NSTextAlignmentRight;
        totalMoneyLabel.text = @"0.01";
        self.totalMoneyLabel = totalMoneyLabel;
        [_footerView addSubview:totalMoneyLabel];
        [totalMoneyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(totalPayLabel);
            make.right.equalTo(_footerView.mas_right).offset(-10 / WIDTH_6S_SCALE);
            make.height.mas_equalTo(20 / HEIGHT_6S_SCALE);
            make.width.mas_equalTo(80 / WIDTH_6S_SCALE);
        }];
        
        // 横线
        UIView *lineView1 = [[UIView alloc]initWithFrame:CGRectZero];
        lineView1.backgroundColor = getColor(@BGCOLOR);
        [_footerView addSubview:lineView1];
        [lineView1 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(weakself.totalMoneyLabel.mas_bottom).offset(10 / HEIGHT_6S_SCALE);
            make.left.right.equalTo(_footerView);
            make.height.mas_equalTo(1 / HEIGHT_6S_SCALE);
        }];
        
        UIButton *sjBtn = [[UIButton alloc]init];
        [sjBtn setBackgroundColor:getColor(whiteColor)];
        [sjBtn setTitleColor:getColor(blackColor) forState:UIControlStateNormal];
        sjBtn.titleLabel.font = DEF_FontSize_14;
        sjBtn.layer.borderColor = getColor(@BGCOLOR).CGColor;
        sjBtn.layer.borderWidth = 1;
        [sjBtn setTitle:@"联系商家" forState:UIControlStateNormal];
        [sjBtn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
        self.sjBtn = sjBtn;
        [_footerView addSubview:sjBtn];
        [sjBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(lineView1.mas_bottom).offset(10 / HEIGHT_6S_SCALE);
            make.left.equalTo(_footerView.mas_left).offset(10 / WIDTH_6S_SCALE);
            make.height.mas_equalTo(40 / HEIGHT_6S_SCALE);
            make.width.mas_equalTo((SCREEN_WIDTH - 40 / WIDTH_6S_SCALE) / 3);
        }];
        UIButton *gkBtn = [[UIButton alloc]init];
        [gkBtn setBackgroundColor:getColor(@"1EBEA8")];
        [gkBtn setTitleColor:getColor(blackColor) forState:UIControlStateNormal];
        gkBtn.titleLabel.font = DEF_FontSize_14;
        [gkBtn setTitle:@"联系顾客" forState:UIControlStateNormal];
        [gkBtn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
        self.gkBtn = gkBtn;
        [_footerView addSubview:gkBtn];
        [gkBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(lineView1.mas_bottom).offset(10 / HEIGHT_6S_SCALE);
            make.left.equalTo(sjBtn.mas_right).offset(10 / WIDTH_6S_SCALE);
            make.height.mas_equalTo(40 / HEIGHT_6S_SCALE);
            make.width.mas_equalTo((SCREEN_WIDTH - 40 / WIDTH_6S_SCALE) / 3);
        }];
        UIButton *makeSureBtn = [[UIButton alloc]init];
        [makeSureBtn setBackgroundColor:getColor(@"F7AB3B")];
        [gkBtn setTitleColor:getColor(blackColor) forState:UIControlStateNormal];
        makeSureBtn.titleLabel.font = DEF_FontSize_14;
        [makeSureBtn setTitle:@"确认取货" forState:UIControlStateNormal];
        [makeSureBtn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
        self.makeSureBtn = makeSureBtn;
        [_footerView addSubview:makeSureBtn];
        [makeSureBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(lineView1.mas_bottom).offset(10 / HEIGHT_6S_SCALE);
            make.left.equalTo(gkBtn.mas_right).offset(10 / WIDTH_6S_SCALE);
            make.height.mas_equalTo(40 / HEIGHT_6S_SCALE);
            make.width.mas_equalTo((SCREEN_WIDTH - 40 / WIDTH_6S_SCALE) / 3);
        }];
        // 横线
        UIView *lineView2 = [[UIView alloc]initWithFrame:CGRectZero];
        lineView2.backgroundColor = getColor(@BGCOLOR);
        [_footerView addSubview:lineView2];
        [lineView2 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(makeSureBtn.mas_bottom).offset(10 / HEIGHT_6S_SCALE);
            make.left.right.equalTo(_footerView);
            make.height.mas_equalTo(15 / HEIGHT_6S_SCALE);
        }];
        UIButton *bottomBtn = [[UIButton alloc]init];
        bottomBtn.backgroundColor = getColor(whiteColor);
        [bottomBtn setTitleColor:getColor(textColor) forState:UIControlStateNormal];
        bottomBtn.titleLabel.font = DEF_FontSize_14;
        bottomBtn.titleLabel.textAlignment = NSTextAlignmentLeft;
//        [bottomBtn setTitle:@"新民市东环街41号" forState:UIControlStateNormal];
        [bottomBtn setImage:[UIImage imageNamed:@"ip"] forState:UIControlStateNormal];
        [bottomBtn setImageEdgeInsets:UIEdgeInsetsMake(0, -210 / WIDTH_6S_SCALE, 0, 0)];
        [bottomBtn setTitleEdgeInsets:UIEdgeInsetsMake(0, -200 / WIDTH_6S_SCALE, 0, 0)];
        [bottomBtn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
        self.bottomBtn = bottomBtn;
        [_footerView addSubview:bottomBtn];
        [bottomBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(lineView2.mas_bottom).offset(5 / HEIGHT_6S_SCALE);
            make.left.right.equalTo(_footerView);
            make.height.mas_equalTo(20 / HEIGHT_6S_SCALE);
        }];
        
    }
    return _footerView;
}
/**
 *  拨打电话
 *
 *  @param phoneNumber 号码
 */
- (void)callPhone:(NSString *)phoneNumber{
    UIAlertController *av = [UIAlertController alertControllerWithTitle:@"拨打电话" message:nil preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"tel://%@",phoneNumber]]];
    }];
    UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    [av addAction:action1];
    [av addAction:action2];
    [self presentViewController:av animated:YES completion:nil];
}
- (NSArray *)dataArray
{
    if (!_dataArray) {
        _dataArray = [NSArray array];
    }
    return _dataArray;
}
@end
